home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Univers Mac Interactif 42
/
Univers Mac Interactif - Issue 42.iso
/
Internet
/
MacHTTP 2.0
/
MacHTTP Software & Docs
/
Tutorials
/
Extending MacHTTP Scripts
/
Script5.txt
< prev
Wrap
Text File
|
1994-12-11
|
6KB
|
128 lines
-- set these properties outside of the event. That way they will only be set
-- once each time the app is run, not once for each event.
property crlf : (ASCII character 13) & (ASCII character 10)
-- This is a standard header for HTML files.
property http_10_header : "HTTP/1.0 200 OK" & crlf & "Server: MacHTTP" & crlf & ¬
"MIME-Version: 1.0" & crlf & "Content-type: text/html" & crlf & crlf
-- Idletime is how long you want it to remain open to
-- wait for another event. Idletime is in seconds.
-- Datestamp will contain the current date. Initialize it here.
property idletime : 300 -- set to 5 minutes
property datestamp : 0
-- this bit of code outside the sdoc event is executed at launch-time
-- it is neccesary because an idle event can happen between
-- launch and the receipt of an sdoc event (and in fact often does)
set datestamp to current date
-- This is the loop for AppleEvents received by the application.
-- When you check the syntax, AppleScript will ask you to locate
-- MacHTTP and will set the correct name at that time.
on «event WWWΩsdoc» path_args ¬
given «class kfor»:http_search_args, «class post»:post_args, «class meth»:method, «class addr»:client_address, «class user»:username, «class pass»:password, «class frmu»:from_user, «class svnm»:server_name, «class svpt»:server_port, «class scnm»:script_name, «class ctyp»:content_type
-- Variables available for use:
-- http_search_args - stuff in the URL after a ?
-- post_args - stuff in the URL after a $
-- method - GET, POST, etc. Used to tell if post_args are valid
-- client_address - IP address or domain name of remote client's host
-- from_user - non-standard. e-mail address of remote user
-- username - authenticated user name
-- password - authenticated password
-- server_name - name or IP address of this server
-- server_port - TCP/IP port number being used by this server
-- script_name - URL name of this script
-- content_type - MIME content type of post_args
-- Using the "try" clause causes the "on error" routine to be run
-- if an error occurs instead of crashing.
-- If the error occurs outside the "try"/"end try" space then
-- the "on error" routine is NOT run.
try
-- save the current date and time to check later for quitting
set datestamp to current date
-- I don't return all of the parameters because most are uninteresting or
-- are not supported by all clients.
-- In addition, we only parse the post_args, since this is the only
-- data entered by the user (all else supplied by MacHTTP).
set return_page to http_10_header ¬
& ¬
"<HTML><HEAD><TITLE>Parsed Results</TITLE></HEAD>" & "<BODY><H1>Parsed Results</H1>" & return ¬
& "<H4>http_search_args</H4>" & return ¬
& http_search_args & return
set return_page to return_page & "<H4>post_args</H4><PRE>" & return
-- this creates a list of "name=value" pairs
set postarglist to tokenize post_args with delimiters {"&"}
-- The section below converts the list to readable output, putting each item onto a separate line.
-- Uses DecodeURL to convert %XX encodings and Tokenize/JoinList to
-- convert "+" to spaces.
-- store the original AppleScript text item delimiters
set oldDelim to AppleScript's text item delimiters
-- use "=" to delimit the pairs
set AppleScript's text item delimiters to {"="}
-- initialize postargtext in case the item is empty
set postargtext to ""
-- traverse the list item by item
repeat with currpostarg in postarglist
set postargtext to postargtext & ¬
(Decode URL (DePlus (last text item of currpostarg))) & return & return
end repeat
-- restore the old AppleScript text item delimiters settings
set AppleScript's text item delimiters to oldDelim
set return_page to return_page ¬
& postargtext & "</PRE>" & return ¬
& "<H4>client_address</H4>" & return ¬
& client_address & return
set return_page to return_page ¬
& "<HR><I>Results generated at: " & (current date) ¬
& "</I>" & "</BODY></HTML>"
-- return the page created. A return statement ends the
-- processing of the AppleEvent
return return_page
-- here is the routine to run if an error occurs
-- errMsg contains the message sent by the System
-- errNum contains the number of the error (negative for System, AE, or AS errors)
on error errMsg number errNum
-- create a page of HTML text to return
set return_page to http_10_header ¬
& "<HTML><HEAD><TITLE>Error Page</TITLE></HEAD>" ¬
& "<BODY><H1>Error Encountered!</H1>" & return ¬
& "An error was encountered while trying to run this script." & return
set return_page to return_page ¬
& "<H3>Error Message</H3>" & return & errMsg & return ¬
& "<H3>Error Number</H3>" & return & errNum & return ¬
& "<H3>Date</H3>" & return & (current date) & return
set return_page to return_page ¬
& "<HR>Please notify Jon Wiederspan at " ¬
& "<A HREF=\"mailto:jonwd@tjp.washington.edu\">jonwd@tjp.washington.edu</A>" ¬
& " of this error." & "</BODY></HTML>"
-- return the page created. A return statement ends the
-- processing of the AppleEvent
return return_page
end try
end «event WWWΩsdoc»
-- The idle function is run everytime the system sends an idle message.
-- If the current date is more than idletime seconds more than the last date
-- then it is time to quit.
-- The return value tells the system how long to wait before
on idle
if (current date) > (datestamp + idletime) then
quit
end if
return 5
end idle
-- This code allows you to do any clean-up that might be necessary.
-- Example: you could write the quit event time to a log to see
-- how often the applet is running.
on quit
-- do any clean-up chores here
continue quit
end quit